# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 商品评论情感分析.py
# @Author: dongguangwen
# @Date  : 2025-02-08 19:11
# 0.导入工具包
import pandas as pd
import numpy as np
import jieba
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB


# 1.加载数据
data = pd.read_csv('./data/书籍评价.csv', encoding='gbk')
# print(data.head())
# print(data.info())

data['labels'] = np.where(data['评价'] == '好评', 1, 0)
# print(data.head())
y = data['labels']

stop_words = []
with open('./data/stopwords.txt', 'r', encoding='utf-8') as file:
    lines = file.readlines()
    stop_words = [line.strip() for line in lines]

stop_words = list(set(stop_words))
# print(stop_words)

# 分词
word_list = [','.join(jieba.lcut(line)) for line in data['内容']]
# print(word_list)

# 词频统计
transform = CountVectorizer(stop_words=stop_words)
x = transform.fit_transform(word_list)
names = transform.get_feature_names_out()
# print(names)
print(len(names))
x = x.toarray()
# print(x)

# 准备训练集   这里将文本前10行当做训练集  后3行当做测试集
x_train = x[:10, :]
y_train = y.values[0: 10]
x_test = x[10:, :]
y_test = y.values[10:]
print(x_test.shape)

# 模型训练
model = MultinomialNB(alpha=1)  # 构建贝叶斯算法分类器
model.fit(x_train, y_train)

y_pred = model.predict(x_test)
print(y_test)
print(y_pred)
print(model.score(x_test, y_test))

"""
37
(3, 37)
[0 0 0]
[0 0 0]
1.0
"""